﻿
    //Copyright 2010 http://tv4home.codeplex.com
    //This file is part of TV4Home.WebInterface.UI.
    //TV4Home.WebInterface.UI is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
    //TV4Home.WebInterface.UI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    //You should have received a copy of the GNU General Public License along with TV4Home.WebInterface.UI If not, see http://www.gnu.org/licenses/
     
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Linq;

namespace TV4Home.WebInterface.UI
{
    public class GetTVProgramsSearchDescription : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            string searchUrl = context.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/SearchTVPrograms.ashx");
            string searchUrlTVGuide = context.Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/TVGuide.aspx");

            context.Response.Clear();

            context.Response.ContentType = "application/opensearchdescription+xml";

            XmlTextWriter xmlWr = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);

            new XDocument(new XElement("OpenSearchDescription",
                                       new XAttribute("version", "2.0"),
                                       new XAttribute("xmlns", "http://a9.com/-/spec/opensearch/1.1/"),
                                       new XElement("ShortName", Properties.Resources.GetTVProgramsSearchDescription_ShortName),
                                       new XElement("Description", Properties.Resources.GetTVProgramsSearchDescription_Description),
                                       new XElement("InputEncoding", "UTF-8"),

                                       new XElement("Url", // add an Url element with type text/html (used for browser search box)
                                                    new XAttribute("type", "text/html"),
                                                    new XAttribute("template", searchUrlTVGuide + "?searchTerm={searchTerms}"),
                                                    new XAttribute("method", "GET")),

                                       new XElement("Url", // add an Url element with type application/rss+xml (used for Windows 7 Federated Search)
                                                    new XAttribute("type", "application/rss+xml"),
                                                    new XAttribute("template", searchUrl + "?term={searchTerms}"),
                                                    new XAttribute("method", "GET")),

                                       new XElement("Url", // add an element for search suggestions in JSON format
                                                    new XAttribute("type", "application/x-suggestions+json"),
                                                    new XAttribute("template", searchUrl + "?term={searchTerms}&searchmode=1"),
                                                    new XAttribute("method", "GET")),

                                       new XElement("Url", // add an element for search suggestions in XML format
                                                    new XAttribute("type", "application/x-suggestions+xml"),
                                                    new XAttribute("template", searchUrl + "?term={searchTerms}&searchmode=2"),
                                                    new XAttribute("method", "GET"))))
                .Save(xmlWr);

            xmlWr.Flush();
            xmlWr.Close();

            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
